using System;

namespace ChessKit.ChessLogics.Internals
{
  internal static class EnPassant
  {
    private static readonly int[] Symbols;
    private const char S = '-';
    private const int Illegal = 99;

    static EnPassant()
    {
      Symbols = new int['z' - S];

      for (var i = 0; i < Symbols.Length; i++)
        Symbols[i] = Illegal;

      Symbols['-' - S] = -1;

      Symbols['a' - S] = 0;
      Symbols['b' - S] = 1;
      Symbols['c' - S] = 2;
      Symbols['d' - S] = 3;
      Symbols['e' - S] = 4;
      Symbols['f' - S] = 5;
      Symbols['g' - S] = 6;
      Symbols['h' - S] = 7;

      Symbols['A' - S] = 0;
      Symbols['B' - S] = 1;
      Symbols['C' - S] = 2;
      Symbols['D' - S] = 3;
      Symbols['E' - S] = 4;
      Symbols['F' - S] = 5;
      Symbols['G' - S] = 6;
      Symbols['H' - S] = 7;
    }

    public static int Parse(char symbol)
    {
      int res;
      try
      {
        res = Symbols[symbol - S];
      }
      catch (IndexOutOfRangeException)
      {
        throw new FormatException("illegal character");
      }
      if (res == Illegal)
        throw new FormatException("illegal character");
      return res;
    }
  }
}